#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.common.component.feign;

import ${package}.common.exception.FeignInterException;
import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * Description: FeginErrorDecoder
 *
 * @author YL
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Slf4j
@Component
public class FeignErrorDecoder implements ErrorDecoder {

    /**
     * Description:
     * <无法补充>
     * @author wupanhua
     * @date 15:19 2019/8/8
     * @param s 1
     * @param response 2
     * @return java.lang.Exception
     **/
    @Override
    public Exception decode(String s, Response response) {

        String message = String.format("status %s reading %s", response.status(), s);

        try {
            if (response.body() != null){
                String body = Util.toString(response.body().asReader(StandardCharsets.UTF_8));
                message = message + "; contents:${symbol_escape}n" + body;
            }
        } catch (IOException e) {
            log.error("Fegin decode [{}] error", s, e);
        }
        return new FeignInterException(response.status(), message);
    }


}
